package com.program.art.refactor;

/**
 * OrderReceipt prints the details of order including customer name, address, description, quantity,
 * price and amount. It also calculates the sales tax @ 10% and prints as part
 * of order. It computes the total order amount (amount of individual lineItems +
 * total sales tax) and prints it.
 * 
 */
public class OrderReceipt {
    private Order order;

    public OrderReceipt(Order order) {
        this.order = order;
	}

	public String printReceipt() {
		StringBuilder output = new StringBuilder();

		printHeader(output);
		printCustomer(output);
		printLineItems(output);
		printSalesTax(output);
		printTotalAmount(output);

		return output.toString();
	}

	private void printTotalAmount(StringBuilder output) {
		double total = 0d;
		for (LineItem lineItem : order.getLineItems()) {
			double salesTax = lineItem.totalAmount() * .10;
			total += lineItem.totalAmount() + salesTax;
		}
		output.append("Total Amount").append('\t').append(total);
	}

	private void printSalesTax(StringBuilder output) {
		double totalSalesTax = 0d;
		for (LineItem lineItem : order.getLineItems()) {
			double salesTax = lineItem.totalAmount() * .10;
			totalSalesTax += salesTax;
		}
		output.append('\n').append("Sales Tax").append('\t').append(totalSalesTax).append('\n');
	}

	private void printLineItems(StringBuilder output) {
		output.append("\n ------ Products ------\n");
		for (LineItem lineItem : order.getLineItems()) {
			output.append(lineItem.getDescription());
			output.append('\t');
			output.append(lineItem.getPrice());
			output.append('\t');
			output.append(lineItem.getQuantity());
			output.append('\t');
			output.append(lineItem.totalAmount());
			output.append('\n');
		}
	}

	private void printCustomer(StringBuilder output) {
		output.append(order.getCustomerName());
		output.append(order.getCustomerAddress());
	}

	private void printHeader(StringBuilder output) {
		output.append("====== Order Receipt ======\n");
	}
}